package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.Func;
import com.company.project.common.utils.StringUtil;
import com.company.project.dto.TThrbEntityDto;
import com.company.project.entity.SysDept;
import com.company.project.entity.SysUser;
import com.company.project.entity.THwtjEntity;
import com.company.project.mapper.SysDeptMapper;
import com.company.project.mapper.THwtjMapper;
import com.company.project.mapper.TWdkhMapper;
import com.company.project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.TThrbMapper;
import com.company.project.entity.TThrbEntity;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("tThrbService")
public class TThrbServiceImpl extends ServiceImpl<TThrbMapper, TThrbEntity> implements TThrbService {

    @Autowired
    public TThrbMapper thrbMapper;
    @Resource
    private UserService userService;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private RoleService roleService;
    @Resource
    private DeptService deptService;
    @Resource
    private TWdkhMapper wdkhMapper;

    @Override
    public DataResult listByPage(Page page, TThrbEntity tThrb) {
        IPage<TThrbEntityDto> iPage = null;
        //通过access_token拿userId
        String userId = httpSessionService.getCurrentUserId();
        SysUser sysUser = userService.getById(userId);
        SysDept sysDept = sysDeptMapper.selectOne(Wrappers.<SysDept>lambdaQuery()
                .eq(SysDept::getId, sysUser.getDeptId()));
        String roleName = roleService.getRoleNames(userId).get(0);

        if (!Func.equals("代理商",roleName)) {
            tThrb.setDeptNo(sysDept.getDeptNo());
        }
        ////4.4.1.超级管理员、代理商角色
        //if (deptName.contains("管理")||deptName.contains("代理")){
        //    iPage = thrbMapper.getListDL(page, tThrb);
        //}else if (deptName.contains("总经理")||deptName.contains("总监")||deptName.contains("主管")){
        //    //4.4.2.总经理页面(总监、主管)页面
        //    iPage = thrbMapper.getListZJL(page, tThrb);
        //}else {
        //    //4.4.3.员工页面
        //    iPage = thrbMapper.getListKHJL(page, tThrb);
        //}
        iPage = thrbMapper.getListDL(page, tThrb,sysUser,roleName);
        return DataResult.success(iPage);
    }

    @Override
    public DataResult infoData() {
        try {
            String userId = httpSessionService.getCurrentUserId();
            SysUser sysUser = userService.getById(userId);
            String username = sysUser.getUsername();
            String roleName = roleService.getRoleNames(userId).get(0);
            SysDept sysDept = sysDeptMapper.selectOne(Wrappers.<SysDept>lambdaQuery()
                    .eq(SysDept::getId, sysUser.getDeptId()));
            Map<String, List<String>> map = new HashMap<>();
            String deptNo = "";
            if (!Func.equals("代理商",roleName)) deptNo = sysDept.getDeptNo();
            //请选择公司、请选择部门、请选择销售;
            List<String> company = thrbMapper.selectCompany(roleName,username);
//            List<String> xs = thrbMapper.selectXs(deptNo,roleName,sysUser);
            List<String> xs = wdkhMapper.selectUsername(sysDept.getDeptNo(),sysUser);
            List<String> dept = new ArrayList<>();
            if (StringUtil.equals("代理商",roleName)||StringUtil.equals("总经理",roleName)||StringUtil.equals("总监",roleName)||StringUtil.equals("主管",roleName)){
                dept = thrbMapper.selectOrg(deptNo,roleName,sysUser);
            }

            map.put("company",company);
            map.put("xs",xs);
            map.put("dept",dept);
            return DataResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.fail("初始化选项失败，请联系管理员！");
        }
    }
}